package com.zzx.service.impl;


import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.zzx.dynamicDataSoure.DynamicDataSourceHolder;
import com.zzx.mapper.UserMapper;
import com.zzx.pojo.User;
import com.zzx.service.IMyRedisService;
import com.zzx.service.IUserService;



@Service
public class UserServiceImpl implements IUserService{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 425459760392694841L;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private IMyRedisService myRedisService;
	
	public void saveUser(User user) {
		System.out.println("****保存数据******");
		DynamicDataSourceHolder.putDataSource("master");
		userMapper.saveUser(user);
		System.out.println("**redis:  "+myRedisService.getString("tableName"));
		System.out.println("****保存数据**成功****");
	}

	public void createTable(String tableName) {
		System.out.println("****创建表******");
		DynamicDataSourceHolder.putDataSource("master");
		//DynamicDataSourceHolder.putDataSource("2");
		myRedisService.setString("tableName", tableName);
		userMapper.createTable(tableName);
		System.out.println("**redis:  "+myRedisService.getString("tableName"));
		System.out.println("****创建 成功******");
	}

	public boolean ifExistsTable(String tableName) {
		System.out.println("***判断表是否存在******");	
		DynamicDataSourceHolder.putDataSource("master");
		int num = userMapper.ifExistsTable(tableName);
		if(num>0) {
			return true;
		}
		return false;
	}
	
	public void getRedisValue(String key ) {
		//System.out.println("***redis 批量演示  开始 ******");	
		String strV = (String) myRedisService.getString(key);
		//myRedisService.set
		
		if(StringUtils.isBlank(strV)) {		
//			boolean bflag = myRedisService.setIfAbsent( key , key , 300);
//			if(bflag) {
//				synchronized(key){					
//					//数据库查询 并存入 redis
//					System.out.println("**真正 redis 空 访问数据库！");
//					strV = key+"88" ;
//					myRedisService.setString(key , strV , 1000);					
//				}
//			}
			//myRedisService.setString(key , key+"88" , 2);
			//System.out.println("**redis 空 访问数据库！");
			//synchronized(key){
				set(key);
			//}
			
			//System.out.println(key + "   " + strV);
		}else {
			System.out.println(key + "   " + strV);
		}
		//System.out.println("***redis 批量演示  结束 ******");	
	}
	
	public synchronized  void set(String key) {
		String strV = (String) myRedisService.getString(key);
		if(strV != null){
			System.out.println(key + "   " + strV);
			//数据直接返回
		} else{
			//数据库查询 并存入 redis
			System.out.println("**真正 redis 空 访问数据库！");
			strV = key+"88" ;
			//struct timeval timeout = { 1, 500000 }; // 1.5 seconds
			Random r = new Random(1);
			r.nextInt(1000);
			//strV = .;//数据库查询出来的值。
			if(StringUtils.isBlank(strV)) {
				//数据库没有数据 写入固定的值入 redis
				//myRedisService.setString(key , strV , 1000);
			} else {
				myRedisService.setString(key , strV , 1000);				
			}
		}
	}
	public void setRedisValue(String key,String value) {
		System.out.println("***redis 存入  开始 ******");	
		for(int i=0;i<100;i++) {
			myRedisService.setString(key+i , value+"88" , 1000);
		}
		System.out.println("***redis 存入  开始 ******");	
	}

}
